package com.yu.learn.service;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;

import com.yu.learn.annotation.WxMonitorNotice;
import com.yu.learn.configuration.server.WxNoticeServer;
import lombok.extern.slf4j.Slf4j;

/**
 * 微信企业账号相关操作
 * @author yushijun
 * @date 2018/5/9
 */
@Service
@Slf4j
public class WxNoticeService {


    @Value("${notice.wx.app.agent.id}")
    private String appAgentId;
    @Value("${notice.wx.app.tag.id}")
    private String tagIdArr;

    @Autowired
    RestTemplate restTemplate;
    @Autowired
    WxNoticeServer wxNoticeServer;

    /**
     * 发送应用消息
     */
    @Async
    public void wxNoticeSend(String content) {

        Map<String, Object> param = ImmutableMap.of("appAgentId", appAgentId, "content", content, "tagIdArr",
                Lists.newArrayList(tagIdArr.split(",")));

        JSONObject jsonObject = restTemplate.postForObject(wxNoticeServer.getWxAppNoticeSendUrl(), param,
                JSONObject.class);
        if (jsonObject == null || jsonObject.getInteger("status") == null || jsonObject.getInteger("status") != 0) {
            log.info("微信通知异常result:{}, param:{}", jsonObject, JSON.toJSON(param));
        }

    }

    @WxMonitorNotice(exception = Exception.class)
    public void test() throws Exception {
        throw new Exception("勇哥强！！！");
    }
}
